<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ImagenesController
 *
 * @author MASTER
 */
class ImagenesController extends AppController {

    public $name = 'Imagenes';

    public function beforeFilter() {
        parent::beforeFilter();
        if ((!$this->Session->check('user')) || ($_SESSION['user'][0]['Tipo'] == 'cliente')) {
            $this->Auth->allow('listaimagenes', 'view');
        } elseif (($this->Session->check('user')) && ($_SESSION['user'][0]['Tipo'] == 'admin')) {
            $this->Auth->allow();
        }
    }

    function listaimagenes() {
        $this->set('imagenes', $this->Imagene->find('all'));
        $this->layout = 'ajax';
    }

    function subirimagen() {
        if ($_FILES['imagenefile']) {
            $destino = WWW_ROOT . 'img/uploads/imagene/filename/' . DS;
            if (move_uploaded_file($_FILES['imagenefile']['tmp_name'], $destino . $_FILES['imagenefile']['name'])) {
                $resp = "Imagen subida correctamente";
            } else {
                $resp = "No se pudo subir imagen";
            }
        } else {
            $resp = "Inserte una imagen";
        }
        $this->set('imagenes', $resp);
        $this->layout = 'ajax';
    }

    public function index() {
        if (!empty($this->data)) {
            $this->Imagene->create();
            if ($this->Imagene->save($this->data)) {
                if ($this->RequestHandler->isAjax()) {
                    $this->set('imagenes', 'ajax');
                    $this->layout = 'ajax';
                }
            }
        }
    }

    public function view($id = null) {
        $this->Imagene->id = $id;
        $this->Imagene->recursive = -1;
        $this->set('imagenes', $this->Imagene->read());
        $this->layout = 'ajax';
    }

    function edit($id = null) {
        $this->Imagene->id = $id;
        if ($this->Imagene->save($this->request->data)) {
            $this->set('imagenes', '1');
            $this->layout = 'ajax';
        }
    }

    function delete($id) {

        if ($this->Imagene->delete($id)) {
            $this->set('imagenes', '1');
            $this->layout = 'ajax';
        }
    }

}

?>
